package com.ldcc.report_system.ui.user;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ldcc.report_system.ApplicationConfig;
import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.api.results.PerPersonnel;
import com.ldcc.report_system.api.results.User;
import com.ldcc.report_system.component.UserInfoItemLayout;
import com.ldcc.report_system.databinding.ActivityMyInfoBinding;
import com.ldcc.report_system.util.ActivitySkipUtil;
import com.ldcc.report_system.util.SharedPreferencesUtils;
import com.ldcc.report_system.util.WindowUtils;
import com.ldcc.report_system.util.http.ClientRequestManage;
import com.ldcc.report_system.util.http.HttpObserver;
import com.ldcc.report_system.util.http.ServerResponse;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author wwg
 * @description 我的信息界面
 */
public class MyInfoActivity extends AppCompatActivity {

    private ActivityMyInfoBinding myInfoBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.myInfoBinding = ActivityMyInfoBinding.inflate(getLayoutInflater());
        setContentView(this.myInfoBinding.getRoot());
        // 设置界面顶部为透明
        WindowUtils.setWindowTopTransparent(getWindow());
        this.initPage();
    }

    /**
     * @description 初始化页面
     */
    private void initPage() {
        // 加载数据
        this.loadUserInfo();
        this.loadPerPersonnel();
    }


    /**
     * @description 加载用户信息
     */
    private void loadUserInfo() {
        ClientRequestManage.getInstance().getUserApi().userInfo().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<User>(ReportApplication.getContext(), null) {
                    @Override
                    public void onSuccess(ServerResponse<User> response_entity) {
                        User user = response_entity.getData();
                        // 设置用户名
                        UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.userInfoFamous, user.getName());
                        // 设置性别
                        UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.userInfoGender, user.getGender() == 0 ? "男" : "女");
                        // 所在部门
                        UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.deptName, user.getDeptName());
                        // 现任职务
                        UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.duty, user.getDuty());
                        // 电话
                        UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.mobile, user.getMobile());
                    }
                });
    }

    /**
     * @description 加载用户人事资料
     */
    private void loadPerPersonnel() {
        String userId = (String) SharedPreferencesUtils.getParam(ReportApplication.getContext(), ApplicationConfig.USER_ID_KEY, "");
        ClientRequestManage.getInstance().getUserApi().getPerPersonnelById(userId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<PerPersonnel>(ReportApplication.getContext(), null) {
                    @Override
                    public void onSuccess(ServerResponse<PerPersonnel> response_entity) {
                        PerPersonnel perPersonnel = response_entity.getData();
                        if (perPersonnel != null) {
                            // 民族
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.userInfoFamous, perPersonnel.getEthnic());
                            // 证件号码
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.identityCard, perPersonnel.getIdentityCard());
                            // 学历
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.education, perPersonnel.getEducation());
                            // 学位
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.degree, perPersonnel.getDegree());
                            // 政治面貌
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.politicsStatus, perPersonnel.getPoliticsStatus());
                            // 毕业院校
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.graduationSchool, perPersonnel.getGraduationSchool());
                            // 所学专业
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.major, perPersonnel.getMajor());
                            // 毕业证书编号
                            UserInfoItemLayout.setUserInfoItemValueText(myInfoBinding.graduationNumber, perPersonnel.getGraduationNumber());
                        }
                    }
                });
    }

    /**
     * @description 返回键事件, 返回, 选择日报界面
     */
    @Override
    public void onBackPressed() {
        ActivitySkipUtil.backMainActivityFragment(this, ApplicationConfig.USER_INFO_FRAGMENT);

    }

}